package ahblue.cn.tanzi.ui.home.article;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shehuan.nicedialog.BaseNiceDialog;
import com.shehuan.nicedialog.NiceDialog;
import com.shehuan.nicedialog.ViewConvertListener;
import com.shehuan.nicedialog.ViewHolder;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.Result;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.jmessage.im.util.DialogCreator;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.VipUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加评论
 */
public class AddCommentActivity extends BaseActivity {

    public static final String ARG_ARTICAL_ID = "ARG_ARTICAL_ID";
    public static final String ARG_COMMENT_TYPE = "ARG_COMMENT_TYPE";

    @BindView(R.id.edit_content)
    EditText mEditTextContent;

    private int mArticalId;
    private int mCommentType;

    private Dialog mDialogLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_news_comment_v2);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mArticalId = intent.getIntExtra(ARG_ARTICAL_ID, 0);
        mCommentType = intent.getIntExtra(ARG_COMMENT_TYPE, 0);

        if (mArticalId == 0 || mCommentType == 0) {
            Toast.makeText(this, "参数不正确", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

    @OnClick(R.id.tv_cancel)
    void onClickCancel() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_cancel_confrim)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder viewHolder, BaseNiceDialog dialog) {
                        TextView textViewMessage = viewHolder.getView(R.id.textViewMessage);
                        textViewMessage.setText("是否退出编辑？");

                        viewHolder.setOnClickListener(R.id.textViewConfirm, view -> {
                            AddCommentActivity.this.finish();
                            dialog.dismiss();
                        });
                        viewHolder.setOnClickListener(R.id.textViewCancel, view -> {
                            dialog.dismiss();
                        });
                    }
                })
                .setMargin(50)
                .show(getSupportFragmentManager());
    }

    @OnClick(R.id.tv_send)
    void onClickSend() {
        if (!VipUtils.checkVip(this)) {
            return;
        }
        String content = mEditTextContent.getText().toString();
        if (content.length() == 0) {
            Toast.makeText(this, "评论内容不能为空", Toast.LENGTH_LONG).show();
            return;
        }

        mDialogLoading = DialogCreator.createLoadingDialog(this, "正在上传");
        mDialogLoading.show();

        LoginResultModel model = LoginResultModel.getUserModel(this, LoginResultModel.class);

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.addBigShotArticleComment(mArticalId, mCommentType, content, model.token, mCallAddComment);
    }


    private ApiCallback<Result> mCallAddComment = new ApiCallback<Result>() {
        @Override
        public void onSuccess(Result data) {
            Toast.makeText(AddCommentActivity.this, "评论添加成功", Toast.LENGTH_LONG).show();
            AddCommentActivity.this.finish();

            if (mDialogLoading != null) {
                mDialogLoading.dismiss();
            }
            setResult(RESULT_OK);
            finish();
        }

        @Override
        public void onFailed(String errorMsg) {
            Toast.makeText(AddCommentActivity.this, errorMsg, Toast.LENGTH_LONG).show();
            if (mDialogLoading != null) {
                mDialogLoading.dismiss();
            }
        }
    };
}
